﻿var express = require('express')
var app = express()
var bodyParser = require('body-parser')
var compress = require('compression')
var cookieParser = require("cookie-parser")
var admin = require('./routes/admin.js')
const log = require('./logger')
var logger = require('./logger').logger
const http = require('http')

log.use(app)
//手动记录，可以代替console.log
logger.info('test info 1')

app.use(compress())
app.use(express.static("../opt"))

app.use(express.static("dist"))
app.use(express.static("dist2"))
app.use(express.static("devDocument"))//专门存放开放相关文档
app.use(express.static("models"))//模版
app.use(express.static("sales"))//销售相关文档
app.use(express.static("productDelivery"))//产品交付相关文档
app.use(express.static("afterSales"))//售后服务相关文档
app.use(express.static("uploads"))
app.use(express.static("public"))
app.use(bodyParser.json({ limit: '50mb' }))
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))
app.use(cookieParser())

//路由模块化
app.use('/admin',admin)

//拦截器
app.use(function (err, _req, res, next) {
  //当token验证失败时会抛出如下错误
  if (err.name === 'UnauthorizedError') {
    //这个需要根据自己的业务逻辑来处理（ 具体的err值 请看下面）
    res.status(401).send('invalid token...')
  }
  next()
})

const server = http.createServer(app)

//测试接口服务
server.listen(7878, async function () {
  var host = 'localhost'
  var port = server.address().port
  console.log("应用实例，访问地址为 http://%s:%s", host, port)
})